<template>
  <transition name="move">
    <div v-show="showFlag"
         class="food"
         ref="food">

      <div class="back"
           @click="hide">
        <div class="arrow_lift ">
          <!-- <span class="lt">&lt;</span> -->
          <img src="../../../static/imgs/ret3.png"
               height="64"
               width="64"
               z-index="999" />
          <!-- <span>{{language}}</span> -->
          <!-- <span>{{$t("message.back")}}</span> -->
        </div>
      </div>

      <div class="food-content">
        <div class="image-header">
          <img :src="food.img">
        </div>
        <div class="content">
          <h1 class="title">{{menu_description(food)}}</h1>
          <div class="detail">
          </div>
          <div class="food_details">
            <div class="price">
              <span class="now">{{currencydefault}}{{food.cost}}</span><span class="old"
                    v-show="food.oldPrice">￥{{food.oldPrice}}</span>

              <div class="cartcontrol-wrapper">
                <cart-control @add="addFood"
                              @dec="decFood"
                              :food="food"
                              :cart-foods="cartFoods"></cart-control>
              </div>
            </div>
            <p>{{instructions(food)}}</p>
          </div>

          <transition name="fade">
            <div @click.stop.prevent="addFirst"
                 class="buy"
                 v-show="!food.count || food.count===0">
              加入购物车
            </div>
          </transition>
        </div>

        <div class="info"
             v-show="food.info">
          <h1 class="title">商品信息</h1>
          <p class="text">{{food.info}}</p>
        </div>

      </div>
    </div>
  </transition>
</template>

<script type="text/ecmascript-6">
import BScroll from 'better-scroll'
import Vue from 'vue'
import cartControl from './../cartcontrol/cartcontrol'
import { formatDate } from './../../common/js/date.js'
import bus from '../../store/bus.js'
const ALL = 2

export default {
    components: {
    cartControl
  },
  props: {
    food_type: {
      type: Number
    },
    food: {
      type: Object
    },
    currencydefault: {
      type: String
    },
    cartFoods: {
      type: Array
    }
  },
  data () {
    return {
      showFlag: false,
      selectType: ALL,
      onlyContent: true,
      desc: {
        all: '全部',
        positive: '推荐',
        negative: '吐槽'
      },
      language: '',
      Menu_description: ''
    }
  },

  watch: {
    food: function (newVal, oldVal) { //不能用箭头函数
      this.Menu_description = newVal
    }
  },

  created () {
    this.Menu_description = this.food


    this.classify()
    switch (navigator.language) {
      case 'zh-CN':
        this.language = '点击返回菜单'
        break
      case 'en':
        this.language = 'Click to return to the menu'
        break
      case 'en-US':
        this.languagee = 'en'
        break
      case 'local':
        this.languagee = 'Klicken Sie, um zum Menü zurückzukehren'
        break
      default:

        break
    }
  },
  methods: {

    menu_description (item) {
      switch (this.food_type) {
        case 0:
          return item.name
        case 1:
          return item.en_name
        case 2:
          return item.zh_cn_name
        case 3:
          return item.de_name
      }
    },
    instructions (item) {
      switch (this.food_type) {
        case 0:
          return item.instro;
        case 1:
          return item.zh_desc;
        case 2:
          return item.en_desc;
        case 3:
          return item.de_desc;
      }
    },

    classify () {
      bus.$on("send", res => {
        this.language = res;
      });
    },

    show () {
      this.showFlag = true
      this.selectType = ALL
      this.onlyContent = true
      this.$nextTick(() => {
        if (!this.scroll) {
          this.scroll = new BScroll(this.$refs.food, {
            click: true
          })
        } else {
          this.scroll.refresh()
        }
      })
    },
    hide () {
      this.showFlag = false
    },
    addFirst (event) {
      if (!event._constructed) {
        return
      }
      this.$emit('add', event.target)
      Vue.set(this.food, 'count', 1)
    },
    addFood (food, target) {
      this.$emit('add', food, target)
    },
    decFood (food, num, disableDec) {
      this.$emit('dec', food, num, disableDec)
    },
    needShow (type, text) {
      if (this.onlyContent && !text) {
        return false
      }
      if (this.selectType === ALL) {
        return true
      } else {
        return type === this.selectType
      }
    },
    selectRating (type) {
      this.selectType = type
      this.$nextTick(() => {
        this.scroll.refresh()
      })
    },
    toggleContent () {
      this.onlyContent = !this.onlyContent
      this.$nextTick(() => {
        this.scroll.refresh()
      })
    }
  },
  mounted () {

  },


  beforeDestroy () {
  },

  destroyed () {
  },
  filters: {
    formatDate (time) {
      let date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    }
  },

}
</script>

<style lang="stylus" rel="stylesheet/stylus">
@import '../../common/stylus/mixin';

.food {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 48px;
  z-index: 30;
  width: 100%;
  background: #fff;
  transform: translate3d(0, 0, 0);

  &.move-enter-active, &.move-leave-active {
    transition: all 0.2s linear;
  }

  &.move-enter, &.move-leave-active {
    transform: translate3d(100%, 0, 0);
  }

  .image-header {
    position: relative;
    width: 100%;
    height: 0;
    padding-top: 100%;

    img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }

    .back {
      position: absolute;
      top: 10px;
      left: 5px;
      z-index: 999;

      .arrow_lift {
        /* text-align center */
        min-width: 86px;
        height: 36px;
        line-height: 34px;
        font-size: 20px;
        color: #fff;
        // background: url('../../../static/imgs/return.png') no-repeat;
        background-size: 100% 100%;
        position: absolute;
        top: 10px;
        left: 5px;
        z-index: 99;

        span {
          margin-left: 30px;
          padding-right: 10px;
        }
      }
    }
  }

  .content {
    position: relative;
    padding: 18px;

    .title {
      line-height: 14px;
      margin-bottom: 8px;
      font-weight: 700;
      color: rgb(7, 17, 27);
    }

    .detail {
      margin-bottom: 18px;
      line-height: 10px;
      // height: 10px;
      font-size: 0;

      .sell-count, .rating {
        font-size: 10px;
        color: rgb(147, 153, 159);
      }

      .sell-count {
        margin-right: 12px;
      }
    }

    .price {
      display: flex;
      width: 100%;
      justify-content: space-between;
      font-weight: 700;
      line-height: 24px;

      // padding-bottom: 17px;
      .now {
        margin-right: 8px;
        font-size: 14px;
        color: rgb(240, 20, 20);
      }

      .old {
        text-decoration: line-through;
        font-size: 10px;
        color: rgb(147, 153, 159);
      }
    }

    .cartcontrol-wrapper {
      // position: absolute;
      // right: 12px;
      // bottom: 12px;
    }

    .buy {
      position: absolute;
      right: 18px;
      bottom: 18px;
      z-index: 10;
      height: 24px;
      line-height: 24px;
      padding: 0 12px;
      box-sizing: border-box;
      border-radius: 12px;
      font-size: 10px;
      color: #fff;
      background: rgb(0, 160, 220);
      opacity: 1;

      &.fade-enter-active, &.fade-leave-active {
        transition: all 0.2s;
      }

      &.fade-enter, &.fade-leave-active {
        opacity: 0;
        z-index: -1;
      }
    }
  }

  .info {
    padding: 18px;

    .title {
      line-height: 14px;
      margin-bottom: 6px;
      font-size: 14px;
      color: rgb(7, 17, 27);
    }

    .text {
      line-height: 24px;
      padding: 0 8px;
      font-size: 12px;
      color: rgb(77, 85, 93);
    }
  }

  .rating {
    padding-top: 18px;

    .title {
      line-height: 14px;
      margin-left: 18px;
      font-size: 14px;
      color: rgb(7, 17, 27);
    }

    .rating-wrapper {
      padding: 0 18px;

      .rating-item {
        position: relative;
        padding: 16px 0;
        border-bottom-1px(rgba(7, 17, 27, 0.1));

        .user {
          position: absolute;
          right: 0;
          top: 16px;
          line-height: 12px;
          font-size: 0;

          .name {
            display: inline-block;
            margin-right: 6px;
            vertical-align: top;
            font-size: 10px;
            color: rgb(147, 153, 159);
          }

          .avatar {
            border-radius: 0;
          }
        }

        .time {
          margin-bottom: 6px;
          line-height: 12px;
          font-size: 10px;
          color: rgb(147, 153, 159);
        }

        .text {
          line-height: 16px;
          font-size: 12px;
          color: rgb(7, 17, 27);

          .icon-thumb_up, .icon-thumb_down {
            margin-right: 4px;
            line-height: 16px;
            font-size: 12px;
          }

          .icon-thumb_up {
            color: rgb(0, 160, 220);
          }

          .icon-thumb_down {
            color: rgb(147, 153, 159);
          }
        }
      }

      .no-rating {
        padding: 16px 0;
        font-size: 12px;
        color: rgb(147, 153, 159);
      }
    }
  }
}

.back {
  position: absolute;
  // top: 10px;
  left: 16px;
  z-index: 999;
  bottom: 14px;
  width: 100%;
}

.back .arrow_lift {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 64px;
  height: 64px;
  line-height: 30px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  background-size: 100% 100%;
  position: absolute;
  z-index: 99;
  left: 16px;
  bottom: 14px;
}

.back .arrow_lift .lt {
  margin-left: 0;
  padding-right: 0;
}

.back .arrow_lift img {
  z-index: 999;
}

.back .arrow_lift span {
  color: #DEDEDE;
  font-size: 14px;
}

.food_details {
  display: flex;
  flex-direction: column;
  border-bottom: 1px dashed #dedede;
  padding-bottom: 17px;
  word-wrap: break-word;
}
</style>
